package com.gyms.view.bottomview;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.webkit.WebView;

import com.classic.okhttp.Cons;
import com.classic.okhttp.enums.Enum;
import com.classic.okhttp.services.ICommonService;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.MyApplication;
import com.gyms.R;
import com.gyms.constant.Constants;
import com.gyms.manager.StateUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zhy.autolayout.AutoLinearLayout;

import constant.PicSplit;

/**
 * 分享邀请框
 */
public class ShareDialog extends BaseBottomDialog implements DialogInterface.OnDismissListener {
    private Activity mContext;

    private String mTitle;//  分享的标题
    private String mTagUrl;// 分享的Url
    private String mContent;// 分享的内容

    private UMShareAPI mUMShareAPI;

   /*回调相关*/
   private WebView mWebView;
   private String mCallBackFunStr;//js回调方法
   private final static int S_SHARE_SCCUESS=1;// 分享成功
   private final static int S_SHARE_ERROR=2;// 分享错误
   private final static int S_SHARE_CANCEL=3;// 分享取消
   private final static int S_SHARE_ALL_PLATFORM=0;//标识所有渠道



   private boolean isClickShare;//是否点击渠道分享 引发的dialog消失

    public ShareDialog(Context context, int animationStyles) {
        super(context, R.style.BottomViewTheme_Defalut);
        animationStyle = animationStyles;
        this.mContext= (Activity) context;
        mUMShareAPI =((MyApplication)mContext.getApplication()).mUMShareAPI;
        setOnDismissListener(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_share;
    }

    @Override
    protected void initView() {
        AutoLinearLayout llayoutFriend = (AutoLinearLayout) findViewById(R.id.llayout_friend);
        AutoLinearLayout llayoutCircle = (AutoLinearLayout) findViewById(R.id.llayout_circle);
        AutoLinearLayout llayoutQQ = (AutoLinearLayout) findViewById(R.id.llayout_qq);
        AutoLinearLayout cancel = (AutoLinearLayout) findViewById(R.id.llayout_cancle);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        llayoutFriend.setTag(R.id.share_dialog, Constants.S_TYPE_WEIXIN);
        llayoutFriend.setOnClickListener(clickListener);
        llayoutCircle.setTag(R.id.share_dialog, Constants.S_TYPE_CIRCLE);
        llayoutCircle.setOnClickListener(clickListener);
        llayoutQQ.setTag(R.id.share_dialog, Constants.S_TYPE_QQ);
        llayoutQQ.setOnClickListener(clickListener);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isClickShare=true;
            int way = (int) v.getTag(R.id.share_dialog);//分享方式0微信好友，1朋友圈;
                    SHARE_MEDIA platform = null;
                    switch (way){
                        case Constants.S_TYPE_WEIXIN://微信好友
                            platform=SHARE_MEDIA.WEIXIN;
                            break;
                        case Constants.S_TYPE_CIRCLE://微信朋友圈
                            platform=SHARE_MEDIA.WEIXIN_CIRCLE;
                            break;
                        case Constants.S_TYPE_QQ://QQ分享
                            platform=SHARE_MEDIA.QQ;
                            break;
                    }
                    doShare(platform);
            dismiss();
        }
    };


    /**
     * 分享
     */

    public void doShare(SHARE_MEDIA platform) {
       String mUrl=StateUtils.shareUrlAppendUserId(mTagUrl);//拼接userId
        if(!mUMShareAPI.isInstall(mContext, platform)){
            ToastUtil.showToast(mContext,"请先安装"+ StateUtils.getSharePlatform(platform));
            //给h5回调
            doJsBack(platform,S_SHARE_ERROR);
            return;
        }
        UMWeb web = new UMWeb(mUrl);
        web.setTitle(mTitle);//标题
        web.setThumb(new UMImage(mContext, Cons.SHARE_IMAGE_ICON));  //缩略图
        web.setDescription(mContent);//描述
        new ShareAction(mContext)
                .withMedia(web)
                .setPlatform(platform)
                .setCallback(shareListener)
                .share();
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         *  分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         *  分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastUtil.showToast(mContext,"分享成功了");
            //调用服务器之后再 回调给js
            reportShareRecord(platform);
        }

        /**
         *  分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtil.showToast(mContext,"失败"+t.getMessage());
            doJsBack(platform,S_SHARE_ERROR);
        }

        /**
         *  分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            doJsBack(platform,S_SHARE_CANCEL);
        }
    };

    /**
     * 分享成功后给js回调
     * @param platform 分享渠道
     * @param mShareResult  分享结果
     */

    private void doJsBack(SHARE_MEDIA platform, int mShareResult) {
        if(null==mCallBackFunStr){
            return;
        }
        Enum.EnumShareTypeEnum shareType = StateUtils.getSharePlatformType(platform);
        mWebView.loadUrl(PicSplit.makeFunctionStr(mCallBackFunStr,mShareResult,shareType.value));
        //分享成功后清为null
        mCallBackFunStr=null;
    }

    /**
     * 设置分享的内容
     * @param mContent  分享内容
     */

    public void setShareContent(String mContent){
        this.mContent=mContent;
    }

    /**
     * 分享标题
     * @param mTitle 标题
     */

    public void setTitle(String mTitle){
        this.mTitle=mTitle;
    }

    /**
     * 分享的url
     * @param mTagUrl url
     */

    public void setTagUrl(String mTagUrl){
        this.mTagUrl=mTagUrl;
    }

    /**
     * 上报分享成功次数
     * @param platform 分享的渠道
     */

    private void reportShareRecord(final SHARE_MEDIA platform){
        Enum.EnumShareTypeEnum shareType = StateUtils.getSharePlatformType(platform);
        ICommonService.reportShareRecord(mContext, shareType, false, new ObjectCallback<Object>() {
            @Override
            public void onSuccess(Object response, String message) {
                //js回调
                doJsBack(platform,S_SHARE_SCCUESS);
            }

            @Override
            public void onErrorT(int code, Object response, String errorMessage) {
                //js回调
                doJsBack(platform,S_SHARE_SCCUESS);
            }

            @Override
            public void onHttpErrorCall(int statusCode) {
                //js回调
                doJsBack(platform,S_SHARE_SCCUESS);
            }
        });
    }

    /**
     * 设置 js弹窗回调
     * @param mCallBackFunStr 回调
     * @param wbWebview webview
     */

    public void setCallBack(String mCallBackFunStr, WebView wbWebview) {
        this.mCallBackFunStr=mCallBackFunStr;
        this.mWebView=wbWebview;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if(null==mCallBackFunStr){//无回调就返回
            return;
        }
        if(isClickShare){//点击分享渠道 dialog消失
            isClickShare=false;
            return;
        }

        //点击取消或者外部 dialog外部消失 给h5 回调
        mWebView.loadUrl(PicSplit.makeFunctionStr(mCallBackFunStr,S_SHARE_CANCEL,S_SHARE_ALL_PLATFORM));
        //分享成功后清为null
        mCallBackFunStr=null;
    }
}
